<?php

namespace app\api\controller;

//用户的基类(必须存在user_token)
#use AlibabaCloud\Client\Config\Config;

use think\Config;
use think\Db;
use think\Exception;

class UserBase
{

    //mongo对象
    public $mongo_obj;

    public function __construct()
    {
        try {
            //上线后隐去
            //$_SERVER['HTTP_USERTOKEN'] = Config::get('HTTP_USERTOKEN');

            if (isset($_SERVER['HTTP_USERTOKEN'])) {
                //判断当前用户是不是正常
                //$user_id = Db::name('user')->where()->value('user_id');
                $user_token = $_SERVER['HTTP_USERTOKEN'];

                $arr = verification($user_token);

                //获取到的user_id
                $user_id = $arr['user_data']->user_id;
                //获取当前用户的
                $user_info = Db::name('user')->where(['user_id' => $user_id])->field('user_id,nick_name,head_img,user_unique,is_caution')->find();
            } else {
                throw new Exception('未登录', 1001);
            }

            if ($user_info) {
                $this->user_id = $user_info['user_id'];
                $this->mongo_obj = Db::connect("db_mongo");
                $this->nick_name = $user_info['nick_name'];
                $this->head_img = $user_info['head_img'];
                $this->user_unique = $user_info['user_unique'];
                //是否设置了提醒
                $this->is_caution = $user_info['is_caution'];
            } else {
                throw new Exception('未登录', 1001);
            }

        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }
}